<?php
namespace App\Logic\miner;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use GuzzleHttp\Client;

class Miner
{

    protected static $url = 'http://117.144.13.22:8088/zabbix/api_jsonrpc.php';
    protected static $header = '{"Content-Type":"application/json-rpc"}';
    protected static $token;


    public static function get_token(){

        $client = new Client();
        $opts = [
            'json' => [
                'jsonrpc' => '2.0',
                'method' => 'user.login',
                'params' => [
                    'user'=>'Admin',
                    'password'=>'zabbix!@#',
                ],
                'id' => 1,
                'auth'=> null,
            ]
        ];
        $rsp = $client->post(self::$url,$opts);


        $data = json_decode($rsp->getBody(),true);
        return $data['result'];

    }

    public static function get_hosts($token){

        $k = rand(0,9);
        $host_arr = get_miner_arr();
        $host = $host_arr[$k];
//        $token = self::get_token();
        $client = new Client();
        $opts = [
            'json' => [
                'jsonrpc' => '2.0',
                'method' => 'host.get',
                'params' => [
                    'filter'=>[
                        'host' => [
                            $host
                        ]
                    ]
                ],
                'id' => 1,
                'auth'=> $token,
            ]
        ];
        $rsp = $client->post(self::$url,$opts);
        $data = json_decode($rsp->getBody(),true);
        $arr = $data['result'];
        $host_arr = [];
        foreach($arr as $v){
            $host_arr[] = $v['hostid'];
        }
        return $host_arr;

    }

    public static function get_itemcpuid(){

        $token = self::get_token();
        $host_arr = self::get_hosts($token);

        $client = new Client();
        $opts = [
            'json' => [
                'jsonrpc' => '2.0',
                'method' => 'item.get',
                'params' => [
                    'output' => 'extend',
                    'hostids' => $host_arr,
//                    'serach'  => [
////                        'key_'  => 'system.cpu.util[,user]'
//                    ],
                    'sortfield' => 'name',
                ],
                'auth'=> $token,
                'id' => 1,
            ]
        ];
        $rsp = $client->post(self::$url,$opts);
        $data = json_decode($rsp->getBody(),true);


        $result = $data['result'];
        foreach($result as $value){

            if($value['name'] == 'Available memory'){
                $memory = $value['lastvalue'];
            }
            if($value['name'] == 'CPU user time'){
                $cpu = $value['lastvalue'];
            }

        }
        $mem = $memory/1024/1024/1024;
        $already_use = 256 - $mem;
        $baifenbi = $already_use/256 * 100;
        $arr = [
            'cpu' => floor($cpu).'%',
            'mem' => floor($baifenbi).'%',
        ];
        return $arr;
    }

}





